import { loginAPI } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || ''
  },
  mutations: {
    setToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    removeToken(state) {
      state.token = null
      removeToken()
    }
  },
  actions: {
    async login(ctx, data) {
      // 调用接口
      const { data: { token }} = await loginAPI(data)
      // 提交mutation
      ctx.commit('setToken', token)
    }
  }
}
